// pages/homePageInfo/homePageInfo.js
import throttle from '../../utils/throttle.js';
import Http from '../../utils/http.js';
import { formatDistance, getTimePart } from '../../utils/util.js';
import { hasPermission } from '../../utils/tools';
import { scrollToLeftEnd } from '../../utils/behavior';
const http = new Http();
const app = getApp();

Page({
    behaviors: [scrollToLeftEnd],
    /**
     * 页面的初始数据
     */
    data: {
        swiperHeight: '', //轮播窗口高度
        // 右上角胶囊的高度
        buttonHeight: '',
        // 右上角胶囊据顶部高度
        buttonTop: '',
        scrollHeight: '',
        title: '',
        tabsArr: [
            {
                type: 'FT测试',
                amount: 0,
            },
            {
                type: 'NFC测试',
                amount: 0,
            },
            {
                type: '运营商',
                amount: 0,
            },
            {
                type: '车食住行',
                amount: 0,
            },
        ],
        // 当地经验数
        localExperienceAmount: 0,
        // 首页tabs索引
        currentTabIndex: 0,
        //
        // FT测试路线列表
        pageNumFTLine: 0,
        loadingFTLine: false,
        loadingAllFTLine: false,
        locationArrFTLine: [],
        totalFTLine: 0,
        // FT测试路线描述
        desArrFTLine: [],
        // FT测试路线封面
        picArrFTLine: [],
        // FT测试路线id
        idArrFTLine: [],
        //
        // FT测试点列表
        locationArrFT: [],
        pageNumFT: 0,
        loadingFT: false,
        loadingAllFT: false,
        totalFT: 0,
        // FT测试筛选距离远近, ''表示不以距离远近作为筛选条件，'asc'表示距离正序，'desc'表示距离倒序
        farOrNearFT: 'asc',
        // FT测试筛选时间早晚
        soonerOrLaterFT: '',
        //
        // NFC测试路线列表
        pageNumNFCLine: 0,
        loadingNFCLine: false,
        loadingAllNFCLine: false,
        locationArrNFCLine: [],
        totalNFCLine: 0,
        // NFC测试路线描述
        desArrNFCLine: [],
        // NFC测试路线封面
        picArrNFCLine: [],
        // NFC测试路线id
        idArrNFCLine: [],
        //
        // NFC测试点列表
        locationArrNFC: [],
        pageNumNFC: 0,
        loadingNFC: false,
        loadingAllNFC: false,
        // NFC测试筛选距离远近, ''表示不以距离远近作为筛选条件，'asc'表示距离正序，'desc'表示距离倒序
        farOrNearNFC: 'asc',
        // NFC测试筛选时间早晚
        soonerOrLaterNFC: '',
        // 易出问题点出行方式
        transportType: {
            '01': '地铁',
            '02': '公交',
            '03': 'BRT',
            '04': '出租车',
            '05': '轮渡',
            '06': '其他',
        },
        area: '',
        // 运营商tabs索引
        networkOperatorsIndex: 0,
        // 运营商数组
        operatorArr: [],
        // 营业厅
        businesshallArr: [],
        // 套餐
        packageArr: [],
        packageId: '',
        //
        // 车食住行下拉筛选
        // 车食住行类型，车01 食02 住03 行04，传空就是查全部
        filterOptions: [
            {
                text: '全部',
                value: 0,
                type: '',
                left: 48,
                count: true,
            },
            {
                text: '租车',
                value: 1,
                count: 0,
                type: '01',
                left: 198,
            },
            {
                text: '住宿',
                value: 2,
                count: 0,
                type: '03',
                left: 348,
                networkspeed: '',
            },
            {
                text: '出行',
                value: 3,
                count: 0,
                type: '04',
                left: 498,
            },
            {
                text: '美食',
                value: 4,
                count: 0,
                type: '02',
                left: 648,
            },
        ],
        // 当前筛选的类型
        filterValue: 0,
        // 筛选是否展开
        filterOpen: false,
        // 车食住行数组
        cszxArr: [],
        pageNumCSZX: 0,
        loadingCSZX: false,
        loadingAllCSZX: false,
        // 车食住行操作
        actions: [
            {
                name: '编辑',
                color: '#333333',
                id: 2,
            },
            {
                name: '删除',
                color: '#FF455E',
                id: 1,
            },
        ],
        show: false,
        cszxId: '',
        activeCreateUid: '',
        activeType: '',
        activeIndex: null,
        questionAnswerNum: 0, //秘书团问答人数
        permissionList: ['00', '01', '04', '05', '06'],
    },

    onLoad: function (options) {
        this.setData({
            currentTabIndex: options.index,
            title: app.globalData.addressBack,
        });

        let isSafeBottom = app.globalData.isIpx;

        var that = this;
        //  高度自适应
        wx.getSystemInfo({
            success: function (res) {
                let clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth,
                    swiperHeight;
                if (isSafeBottom) {
                    swiperHeight = clientHeight * rpxR - 360;
                } else {
                    swiperHeight = clientHeight * rpxR - 328;
                }

                that.setData({
                    swiperHeight: swiperHeight,
                });
            },
        });
        // 头像组件调用方法
        that.headEvent = that.selectComponent('#headEvent');
        that.headEvent.getImgInfo();

        // 获取顶部数字
        this.getTabsCount();
        // 获取FT测试点列表
        this.getFtSpecialLocationList();
        // 获取FT测试路线列表
        this.getFtTestLineList();
        // 获取NFC测试路线列表
        this.getNfcTestLineList();
        // 获取NFC测试点列表
        this.getNfcTestPointList();
        // 获取运营商列表
        this.getCityOperatorList();
        // 获取车食住行
        this.getCSZXList();
        // 获取营业厅
        this.getBusinesshallList();
        // 获取套餐
        this.getPackageList();
    },

    // 从自定义导航栏组件中获取气泡信息
    getMenuButtonBoundingClientRect(e) {
        this.setData({
            buttonHeight: e.detail.height,
            buttonTop: e.detail.top,
        });
        let removeHeight = e.detail.top + e.detail.height + 5;
        this.getScrollHeight(removeHeight);
    },

    getScrollHeight(removeHeight) {
        var that = this;
        wx.getSystemInfo({
            success: function (res) {
                let clientHeight = res.windowHeight,
                    scrollHeight;
                const bottomHeight = app.globalData.isIpx ? 71 : 54;
                const upperHeight = 117;
                // 页面高度减去需要删除的高度(包括自定义导航栏、秘书团加tabs、底部按钮)
                scrollHeight =
                    clientHeight - removeHeight - upperHeight - bottomHeight;

                that.setData({
                    scrollHeight: scrollHeight,
                });
            },
        });
    },
    stopGreyBoxEvent(e) {
        this.setData({
            filterOpen: !this.data.filterOpen,
        });
        return;
    },
    back() {
        wx.navigateBack({
            delta: 1,
        });
    },

    onTabsItemTap: function (event) {
        let index = event.currentTarget.dataset.index;
        this.setData({
            currentTabIndex: index,
        });
    },

    // 网络运营商tabs点击事件
    onTabsClick(e) {
        this.setData({
            networkOperatorsIndex: e.detail.index,
        });
    },

    toggle(e) {
        let kind = e.target.dataset.kind;
        this.setData({
            [kind]: !this.data[kind],
        });
    },

    changeNav: throttle(
        (e) => {
            let finish = e.currentTarget.dataset.finish;
            if (finish) {
                wx.showToast({
                    title: '此功能暂未开放',
                    icon: 'none',
                    duration: 2000,
                });
            } else {
                let { nav, dotid, mtitle } = e.currentTarget.dataset;
                if (dotid || mtitle) {
                    wx.showToast({
                        title: '请到春军扫网App查看',
                        icon: 'none',
                        duration: 1500,
                    });
                    return;
                }
                console.log(nav);
                wx.navigateTo({
                    url: nav,
                });
            }
        },
        500,
        {
            trailing: false,
        }
    ),

    swiperChange(e) {
        console.log(e.detail.source);
        if (e.detail.source === 'touch') {
            this.setData({
                currentTabIndex: e.detail.current,
            });
        }
    },

    // 获取顶部数字
    getTabsCount() {
        let _that = this;
        let country;
        if (app.globalData.countryBack == '中国') {
            if (app.globalData.addressBack == '香港') {
                country = '中国香港';
            } else if (
                app.globalData.taiwan.includes(app.globalData.addressBack)
            ) {
                country = '中国台湾';
            } else if (app.globalData.addressBack == '澳门') {
                country = '中国澳门';
            } else {
                country = '中国大陆';
            }
        } else {
            country = app.globalData.countryBack;
        }
        wx.request({
            url: app.globalData.rootPath + 'code/getMapPointCnt',
            data: {
                city: app.globalData.addressBack,
                country: app.globalData.countryBack,
                region: country,
            },
            success(res) {
                if (res.data.status === '0') {
                    res.data.data.forEach((item) => {
                        if (item.type === '01') {
                            _that.data.tabsArr[0].amount = item.num;
                        }
                        if (item.type === '02') {
                            _that.data.tabsArr[1].amount = item.num;
                        }
                        if (item.type === '03') {
                            _that.data.tabsArr[2].amount = item.num;
                        }
                        if (item.type === '04') {
                            _that.data.tabsArr[3].amount = item.num;
                        }
                        if (item.type === '05') {
                            _that.data.localExperienceAmount = item.num;
                        }
                    });
                    _that.setData({
                        tabsArr: _that.data.tabsArr,
                        localExperienceAmount: _that.data.localExperienceAmount,
                    });
                } else {
                }
            },
        });
    },

    // 获取FT特殊点列表
    getFtSpecialLocationList() {
        let _that = this;
        if (!_that.data.loadingAllFT && !_that.data.loadingFT) {
            _that.setData({
                pageNumFT: _that.data.pageNumFT + 1,
                loadingFT: true,
                loadingAllFT: false,
            });
            let data = {
                authId: app.globalData.authId,
                pageSize: 20,
                pageNo: _that.data.pageNumFT,
                city: app.globalData.addressBack,
                country: app.globalData.countryBack,
                distanceorder: _that.data.farOrNearFT,
                updatetimeorder: _that.data.soonerOrLaterFT,
            };
            http.getRequest(`cjMap/getFtSpecialLocationList`, data).then(
                (res) => {
                    if (res.status == 0) {
                        res.list.map((item) => {
                            item.createtime = app.timeago(item.createtime);
                            item.distance = formatDistance(item.distance);
                            item.operatorslogo = [];
                            // 处理运营商信息
                            if (item.operatorpicurl) {
                                item.operatorpicurl
                                    .split(',')
                                    .map((operator) => {
                                        item.operatorslogo.push(operator);
                                    });
                            }
                            _that.data.locationArrFT.push(item);
                        });
                        _that.setData({
                            locationArrFT: _that.data.locationArrFT,
                            loadingFT: false,
                            totalFT: res.pager.totalRows,
                        });

                        if (res.pager.currentPage >= res.pager.totalPages) {
                            _that.setData({
                                loadingAllFT: true,
                            });
                        }
                    } else {
                        _that.setData({
                            loadingAllFT: true,
                            loadingFT: false,
                        });
                    }
                }
            );
        }
    },

    // 获取FT测试路线
    getFtTestLineList() {
        let _that = this;
        if (!_that.data.loadingAllFTLine && !_that.data.loadingFTLine) {
            _that.setData({
                pageNumFTLine: _that.data.pageNumFTLine + 1,
                loadingFTLine: true,
                loadingAllFTLine: false,
            });
            let data = {
                authId: app.globalData.authId,
                pageSize: 20,
                pageNo: _that.data.pageNumFTLine,
                city: app.globalData.addressBack,
                country: app.globalData.countryBack,
                effectiveflg: '00',
            };
            http.getRequest(`cjMap/getFtTestLineList`, data).then((res) => {
                if (res.status == 0) {
                    res.list.map((item) => {
                        item.createtime = app.timeago(item.createtime);
                        _that.data.locationArrFTLine.push(item);
                        _that.data.desArrFTLine.push({
                            title: item.linename,
                            content: (item.operator || '') + item.networktype,
                        });
                        _that.data.picArrFTLine.push(
                            JSON.parse(item.picurl)[0]
                        );
                        _that.data.idArrFTLine.push(item.id);
                    });
                    _that.setData({
                        locationArrFTLine: _that.data.locationArrFTLine,
                        loadingFTLine: false,
                        totalFTLine: res.pager.totalRows,
                        desArrFTLine: _that.data.desArrFTLine,
                        picArrFTLine: _that.data.picArrFTLine,
                        idArrFTLine: _that.data.idArrFTLine,
                    });

                    if (res.pager.currentPage >= res.pager.totalPages) {
                        _that.setData({
                            loadingAllFTLine: true,
                        });
                    }
                } else {
                    _that.setData({
                        loadingAllFTLine: true,
                        loadingFTLine: false,
                    });
                }
            });
        }
    },

    // 获取NFC测试路线
    getNfcTestLineList() {
        let _that = this;
        if (!_that.data.loadingAllNFCLine && !_that.data.loadingNFCLine) {
            _that.setData({
                pageNumNFCLine: _that.data.pageNumNFCLine + 1,
                loadingNFCLine: true,
                loadingAllNFCLine: false,
            });
            let data = {
                authId: app.globalData.authId,
                pageSize: 20,
                pageNo: _that.data.pageNumNFCLine,
                city: app.globalData.addressBack,
                country: app.globalData.countryBack,
                effectiveflg: '00',
            };
            http.getRequest(`cjMap/getNfcTestLineList`, data).then((res) => {
                if (res.status == 0) {
                    res.list.map((item) => {
                        item.createtime = app.timeago(item.createtime);
                        _that.data.locationArrNFCLine.push(item);

                        let contentStr =
                            (item.nfcwallet === '00' ? 'NFC钱包、' : '') +
                            (item.handheld === '00' ? '手持设备、' : '') +
                            (item.bomtest === '00' ? 'BOM测试、' : '') +
                            (item.nfcflashpay === '00' ? 'NFC云闪付、' : '') +
                            (item.selfcheckticket === '00'
                                ? '自助查票机、'
                                : '');

                        _that.data.desArrNFCLine.push({
                            title: item.linename,
                            content: contentStr.substr(
                                0,
                                contentStr.length - 1
                            ),
                        });
                        _that.data.picArrNFCLine.push(
                            JSON.parse(item.picurl)[0]
                        );
                        _that.data.idArrNFCLine.push(item.id);
                    });
                    _that.setData({
                        locationArrNFCLine: _that.data.locationArrNFCLine,
                        loadingNFCLine: false,
                        totalNFCLine: res.pager.totalRows,
                        desArrNFCLine: _that.data.desArrNFCLine,
                        picArrNFCLine: _that.data.picArrNFCLine,
                        idArrNFCLine: _that.data.idArrNFCLine,
                    });

                    if (res.pager.currentPage >= res.pager.totalPages) {
                        _that.setData({
                            loadingAllNFCLine: true,
                        });
                    }
                } else {
                    _that.setData({
                        loadingAllNFCLine: true,
                        loadingNFCLine: false,
                    });
                }
            });
        }
    },

    // 获取NFC测试点列表
    getNfcTestPointList() {
        let _that = this;
        if (!_that.data.loadingAllNFC && !_that.data.loadingNFC) {
            _that.setData({
                pageNumNFC: _that.data.pageNumNFC + 1,
                loadingNFC: true,
                loadingAllNFC: false,
            });
            let data = {
                authId: app.globalData.authId,
                pageSize: 20,
                pageNo: _that.data.pageNumNFC,
                city: app.globalData.addressBack,
                country: app.globalData.countryBack,
                distanceorder: _that.data.farOrNearNFC,
                updatetimeorder: _that.data.soonerOrLaterNFC,
            };
            http.getRequest(`cjMap/getNfcTestPointList`, data).then((res) => {
                if (res.status == 0) {
                    res.list.map((item) => {
                        item.createtime = app.timeago(item.createtime);
                        item.distance = formatDistance(item.distance);
                        item.transporttypes = _that.formatTranssportType(
                            item.transporttype
                        );
                        item.types = item.type.split(',');
                        _that.data.locationArrNFC.push(item);
                    });
                    _that.setData({
                        locationArrNFC: _that.data.locationArrNFC,
                        loadingNFC: false,
                        totalNFC: res.pager.totalRows,
                    });

                    if (res.pager.currentPage >= res.pager.totalPages) {
                        _that.setData({
                            loadingAllNFC: true,
                        });
                    }
                } else {
                    _that.setData({
                        loadingAllNFC: true,
                        loadingNFC: false,
                    });
                }
            });
        }
    },

    formatTranssportType(transporttype) {
        let transsportTypeArray = [];
        if (transporttype.indexOf('01') > -1) {
            transsportTypeArray.push('地铁');
        }
        if (transporttype.indexOf('02') > -1) {
            transsportTypeArray.push('公交');
        }
        if (transporttype.indexOf('03') > -1) {
            transsportTypeArray.push('BRT');
        }
        if (transporttype.indexOf('04') > -1) {
            transsportTypeArray.push('出租车');
        }
        if (transporttype.indexOf('05') > -1) {
            transsportTypeArray.push('轮渡');
        }
        if (transporttype.indexOf('06') > -1) {
            transsportTypeArray.push('其他');
        }

        return transsportTypeArray.join('/');
    },

    // 按距离排序
    sortDistanceFT() {
        if (this.data.farOrNearFT !== 'asc') {
            this.setData({
                farOrNearFT: 'asc',
            });
        } else {
            this.setData({
                farOrNearFT: 'desc',
            });
        }
        this.setData({
            soonerOrLaterFT: '',
        });
        this.refresh(['ftPoint']);
    },

    sortTimeFT() {
        if (this.data.soonerOrLaterFT !== 'asc') {
            this.setData({
                soonerOrLaterFT: 'asc',
            });
        } else {
            this.setData({
                soonerOrLaterFT: 'desc',
            });
        }
        this.setData({
            farOrNearFT: '',
        });
        this.refresh(['ftPoint']);
    },

    // 按距离排序
    sortDistanceNFC() {
        if (this.data.farOrNearNFC !== 'asc') {
            this.setData({
                farOrNearNFC: 'asc',
            });
        } else {
            this.setData({
                farOrNearNFC: 'desc',
            });
        }
        this.setData({
            soonerOrLaterNFC: '',
        });
        this.refresh(['nfcPoint']);
    },

    sortTimeNFC() {
        if (this.data.soonerOrLaterNFC !== 'asc') {
            this.setData({
                soonerOrLaterNFC: 'asc',
            });
        } else {
            this.setData({
                soonerOrLaterNFC: 'desc',
            });
        }
        this.setData({
            farOrNearNFC: '',
        });
        this.refresh(['nfcPoint']);
    },

    // 获取运营商
    getCityOperatorList() {
        let _that = this;
        let area = '';
        let country = app.globalData.countryBack;
        let city = app.globalData.addressBack;

        if (country == '中国') {
            if (['香港', '澳门'].includes(city)) {
                area = country + city;
            } else if (app.globalData.taiwan.includes(city)) {
                area = '中国台湾';
            } else {
                area = '中国大陆';
            }
        } else {
            area = country;
        }

        _that.setData({
            area,
        });

        wx.request({
            url: app.globalData.rootPath + 'cjMap/getCityOperatorList',
            data: {
                authId: app.globalData.authId,
                pageSize: 20,
                pageNo: 1,
                country: area,
                city,
                nation: country,
            },
            success(res) {
                if (res.data.status === '0') {
                    let temp = [];
                    res.data.data.map((operator) => {
                        let tempNetworkSupportArr = [
                            {
                                name: 'NR',
                                value: '00',
                                checked: false,
                            },
                            {
                                name: 'VoLTE',
                                value: '06',
                                checked: false,
                            },
                            {
                                name: 'ViLTE',
                                value: '03',
                                checked: false,
                            },
                            {
                                name: 'VoWiFi',
                                value: '02',
                                checked: false,
                            },
                            {
                                name: 'ViWiFi',
                                value: '01',
                                checked: false,
                            },
                            {
                                name: 'LTE',
                                value: '04',
                                checked: false,
                            },
                            {
                                name: '3G',
                                value: '07',
                                checked: false,
                            },
                            {
                                name: '2G',
                                value: '08',
                                checked: false,
                            },
                        ];
                        if (operator.networksupport.includes('00')) {
                            tempNetworkSupportArr[0].checked = true;
                        }
                        if (operator.networksupport.includes('01')) {
                            tempNetworkSupportArr[4].checked = true;
                        }
                        if (operator.networksupport.includes('02')) {
                            tempNetworkSupportArr[3].checked = true;
                        }
                        if (operator.networksupport.includes('03')) {
                            tempNetworkSupportArr[2].checked = true;
                        }
                        if (operator.networksupport.includes('04')) {
                            if (operator.networksupport.includes('05')) {
                                tempNetworkSupportArr[5].name = 'TD/FDD-LTE';
                            } else {
                                tempNetworkSupportArr[5].name = 'TD-LTE';
                            }
                            tempNetworkSupportArr[5].checked = true;
                        } else if (operator.networksupport.includes('05')) {
                            tempNetworkSupportArr[5].checked = true;
                            tempNetworkSupportArr[5].name = 'FDD-LTE';
                        }
                        if (operator.networksupport.includes('06')) {
                            tempNetworkSupportArr[1].checked = true;
                        }
                        if (operator.networksupport.includes('07')) {
                            tempNetworkSupportArr[6].checked = true;
                            tempNetworkSupportArr[6].name = 'WCDMA';
                        }
                        if (operator.networksupport.includes('08')) {
                            tempNetworkSupportArr[6].checked = true;
                            tempNetworkSupportArr[6].name = 'CDMA2000';
                        }
                        if (operator.networksupport.includes('09')) {
                            tempNetworkSupportArr[6].checked = true;
                            tempNetworkSupportArr[6].name = 'TD-SCDMA';
                        }
                        if (operator.networksupport.includes('10')) {
                            tempNetworkSupportArr[7].checked = true;
                            tempNetworkSupportArr[7].name = 'GSM';
                        }
                        if (operator.networksupport.includes('11')) {
                            tempNetworkSupportArr[7].checked = true;
                            tempNetworkSupportArr[7].name = 'CDMA1x';
                        }
                        operator.apnArr = JSON.parse(operator.apnpoint);
                        operator.networksupport = tempNetworkSupportArr;
                        operator.ussd = JSON.parse(operator.ussd);
                        if (operator.lte) {
                            // 基站 E-UTRAN
                            operator.lte.basestationdevicemanufacturer =
                                operator.lte.basestationdevicemanufacturer
                                    ? operator.lte.basestationdevicemanufacturer.split(
                                          ','
                                      )
                                    : [''];
                            // 核心网 EPC
                            operator.lte.corenetdevicemanufacturer = operator
                                .lte.corenetdevicemanufacturer
                                ? operator.lte.corenetdevicemanufacturer.split(
                                      ','
                                  )
                                : [''];
                            // IP多媒体系统 IMS
                            operator.lte.ipmediadevicemanufacturer = operator
                                .lte.ipmediadevicemanufacturer
                                ? operator.lte.ipmediadevicemanufacturer.split(
                                      ','
                                  )
                                : [''];
                            // 频段支持 Band
                            operator.lte.channelname =
                                operator.lte.channelname || '';
                            // 区域划分界限
                            operator.lte.regionlimit =
                                operator.lte.regionlimit || '';
                        }

                        if (operator.nr) {
                            // 基站 NG-RAN
                            operator.nr.basestationdevicemanufacturer = operator
                                .nr.basestationdevicemanufacturer
                                ? operator.nr.basestationdevicemanufacturer.split(
                                      ','
                                  )
                                : [''];
                            // 核心网 5GC
                            operator.nr.corenetdevicemanufacturer = operator.nr
                                .corenetdevicemanufacturer
                                ? operator.nr.corenetdevicemanufacturer.split(
                                      ','
                                  )
                                : [''];
                            // 频段支持 Band
                            operator.nr.channelname =
                                operator.nr.channelname || '';
                            // 区域划分界限
                            operator.nr.regionlimit =
                                operator.nr.regionlimit || '';
                        }

                        temp.push(operator);
                    });

                    _that.setData({
                        operatorArr: temp,
                    });
                } else {
                    console.log('请求失败');
                }
            },
        });
    },

    // 获取车食住行列表
    getCSZXList() {
        if (!this.data.loadingAllCSZX && !this.data.loadingCSZX) {
            this.setData({
                pageNumCSZX: this.data.pageNumCSZX + 1,
                loadingCSZX: true,
                loadingAllCSZX: false,
            });
            let data = {
                authId: app.globalData.authId,
                pageSize: 20,
                pageNo: this.data.pageNumCSZX,
                city: app.globalData.addressBack,
                country: app.globalData.countryBack,
                type: this.data.filterOptions[this.data.filterValue].type,
            };
            http.getRequest('cjMap/getTFATList', data).then((res) => {
                if (res.status == 0) {
                    if (this.data.pageNumCSZX == 1) {
                        this.setData({
                            cszxArr: [],
                        });
                    }
                    this.data.filterOptions[1].count =
                        res.transportationcnt || 0;
                    this.data.filterOptions[2].networkspeed =
                        res.networkspeed || '';
                    this.data.filterOptions[2].count =
                        res.accommodationcnt || 0;
                    this.data.filterOptions[3].count = res.tripcnt || 0;
                    this.data.filterOptions[4].count = res.foodcnt || 0;
                    let pages = getCurrentPages(); //当前页面栈id
                    let prevPage = pages[pages.length - 2]; //上一页面
                    if (prevPage.networkspeed != res.networkspeed) {
                        prevPage.setData({
                            networkspeed: res.networkspeed,
                        });
                    }
                    res.data.map((item) => {
                        let time = app.timeago(
                            item.createtime == item.updatetime
                                ? item.createtime
                                : item.updatetime
                        );
                        let timeTip =
                            item.createtime == item.updatetime
                                ? ' 发布'
                                : ' 更新';
                        item.createtime = time + timeTip;
                        item.realname =
                            item.createtime == item.updatetime
                                ? item.realname
                                : item.updaterealname;
                        item.ellipsis = item.excontent.length > 88;
                        item.picurls = item.picurl
                            ? JSON.parse(item.picurl)
                            : [];
                        item.typearray = item.type.split(',');
                        this.data.cszxArr.push(item);
                    });

                    this.setData({
                        cszxArr: this.data.cszxArr,
                        loadingCSZX: false,
                        filterOptions: this.data.filterOptions,
                    });

                    if (this.data.filterValue == 0) {
                        let arr = 'filterOptions[0].count';
                        if (!this.data.cszxArr.length) {
                            this.setData({
                                [arr]: false,
                            });
                        } else {
                            this.setData({
                                [arr]: true,
                            });
                        }
                    }
                    if (res.pager.currentPage >= res.pager.totalPages) {
                        this.setData({
                            loadingAllCSZX: true,
                        });
                    }
                } else {
                    this.setData({
                        loadingAllCSZX: true,
                        loadingCSZX: false,
                    });
                }
            });
        }
    },

    // 筛选展开收起
    toggleFilterOpen() {
        this.setData({
            filterOpen: !this.data.filterOpen,
        });
    },

    // 点击筛选
    filterType(e) {
        let { value, count } = e.currentTarget.dataset;
        if (!count || value == this.data.filterValue) return;
        this.setData(
            {
                filterValue: value,
            },
            () => {
                this.refresh(['cszx']);
            }
        );

        this.toggleFilterOpen();
    },

    edit(e) {
        let { uid, id, index, type, istop, istopall } = e.detail;
        let actions = [
            {
                name: '编辑',
                color: '#333333',
                id: 2,
            },
            {
                name: '删除',
                color: '#FF455E',
                id: 1,
            },
        ];
        let topValue = this.data.filterValue ? istop : istopall;
        if (hasPermission(['00', '01', '04', '05'])) {
            if (topValue === '00') {
                actions.splice(0, 0, {
                    name: '置顶',
                    color: '#333333',
                    id: 3,
                });
            } else {
                actions.splice(0, 0, {
                    name: '取消置顶',
                    color: '#333333',
                    id: 4,
                });
            }
        }

        this.setData({
            show: true,
            activeCreateUid: uid,
            activeType: type,
            cszxId: id,
            actions,
            activeIndex: index,
        });
    },

    onCancel() {
        this.setData({
            show: false,
        });
    },

    onSelect(e) {
        // 运营商套餐编辑
        if (this.data.currentTabIndex == 2) {
            if (e.detail.id === 2) {
                wx.navigateTo({
                    url: `../../releasePage/pages/addPackage/addPackage?&id=${this.data.packageId}`,
                });
            } else if (e.detail.id === 1) {
                wx.showModal({
                    title: '提示',
                    content: '确认要删除该项吗？',
                    success: (res) => {
                        if (res.confirm) {
                            this.deletePackage();
                        }
                    },
                });
            }
        }
        // 车食住行编辑
        if (this.data.currentTabIndex == 3) {
            if (e.detail.id === 2) {
                wx.navigateTo({
                    url: `../../releasePage/pages/addFood/addFood?type=02&id=${this.data.cszxId}&cszx=${this.data.activeType}`,
                });
            } else if (e.detail.id === 1) {
                wx.showModal({
                    title: '提示',
                    content: '确认要删除该项吗？',
                    success: (res) => {
                        if (res.confirm) {
                            this.deleteCSZX();
                        }
                    },
                });
            } else if (e.detail.id === 3) {
                this.setCSZXTop('01');
            } else if (e.detail.id === 4) {
                this.setCSZXTop('00');
            }
        }
        this.onCancel();
    },

    // 删除车食住行
    deleteCSZX() {
        let data = {
            authId: app.globalData.authId,
            id: this.data.cszxId,
            type: this.data.activeType,
        };
        http.getRequest('cjMap/deleteTFAT', data).then((res) => {
            if (res.status == 0) {
                this.data.cszxArr.splice(this.data.activeIndex, 1);
                this.setData({
                    cszxArr: this.data.cszxArr,
                });
                let data = {
                    authId: app.globalData.authId,
                    pageSize: 1,
                    pageNo: 1,
                    city: app.globalData.addressBack,
                    country: app.globalData.countryBack,
                    type: this.data.filterOptions[this.data.filterValue].type,
                    lat: app.globalData.locationLat,
                    lng: app.globalData.locationLng,
                };
                http.getRequest('cjMap/getTFATList', data).then((res) => {
                    if (res.status == 0) {
                        this.data.filterOptions[2].networkspeed =
                            res.networkspeed || '';
                        let pages = getCurrentPages(); //当前页面栈id
                        let prevPage = pages[pages.length - 2]; //上一页面
                        if (prevPage.networkspeed != res.networkspeed) {
                            prevPage.setData({
                                networkspeed: res.networkspeed,
                            });
                        }
                        this.setData({
                            filterOptions: this.data.filterOptions,
                        });
                    }
                });
                // 刷新顶部数字点
                this.getTabsCount();
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1500,
                });
            }
        });
    },

    // 设置车食住行置顶，置顶'01'，取消置顶'00'
    setCSZXTop(istop) {
        let data = {
            authId: app.globalData.authId,
            id: this.data.cszxId,
            type: this.data.activeType,
            city: app.globalData.addressBack,
            country: app.globalData.countryBack,
        };
        if (!this.data.filterValue) {
            data.istopall = istop;
        } else {
            data.istop = istop;
        }

        http.getRequest('cjMap/setTFATTop', data).then((res) => {
            if (res.status == 0) {
                this.refresh(['cszx']);
            }
        });
    },

    // 刷新
    refresh(refreshArr) {
        if (!refreshArr) return;
        if (refreshArr.includes('ftLine')) {
            this.setData(
                {
                    pageNumFTLine: 0,
                    loadingFTLine: false,
                    loadingAllFTLine: false,
                    locationArrFTLine: [],
                    desArrFTLine: [],
                    picArrFTLine: [],
                    idArrFTLine: [],
                },
                () => {
                    this.getFtTestLineList();
                }
            );
        }
        if (refreshArr.includes('ftPoint')) {
            this.setData(
                {
                    pageNumFT: 0,
                    loadingFT: false,
                    loadingAllFT: false,
                    locationArrFT: [],
                },
                () => {
                    this.getFtSpecialLocationList();
                }
            );
        }
        if (refreshArr.includes('nfcLine')) {
            this.setData(
                {
                    pageNumNFCLine: 0,
                    loadingNFCLine: false,
                    loadingAllNFCLine: false,
                    locationArrNFCLine: [],
                    desArrNFCLine: [],
                    picArrNFCLine: [],
                    idArrNFCLine: [],
                },
                () => {
                    this.getNfcTestLineList();
                }
            );
        }
        if (refreshArr.includes('nfcPoint')) {
            this.setData(
                {
                    pageNumNFC: 0,
                    loadingNFC: false,
                    loadingAllNFC: false,
                    locationArrNFC: [],
                },
                () => {
                    this.getNfcTestPointList();
                }
            );
        }
        if (refreshArr.includes('count')) {
            this.getTabsCount();
        }
        if (refreshArr.includes('operator')) {
            this.getCityOperatorList();
        }
        if (refreshArr.includes('cszx')) {
            this.setData(
                {
                    cszxArr: [],
                    pageNumCSZX: 0,
                    loadingCSZX: false,
                    loadingAllCSZX: false,
                },
                () => {
                    this.getCSZXList();
                }
            );
        }
        if (refreshArr.includes('businesshall')) {
            this.setData(
                {
                    businesshallArr: [],
                },
                () => {
                    this.getBusinesshallList();
                }
            );
        }
        if (refreshArr.includes('package')) {
            this.setData(
                {
                    packageArr: [],
                },
                () => {
                    this.getPackageList();
                }
            );
        }
    },

    // 刷新某一条经验的评论数
    refreshCommentNum(params) {
        const { id, commentcount, type } = params;
        if (type === 'ftPoint') {
            const index = this.data.locationArrFT.findIndex((item) => {
                return item.id === id;
            });
            if (index > -1) {
                this.setData({
                    [`locationArrFT[${index}].commentcount`]: commentcount,
                });
            }
        } else if (type === 'nfcPoint') {
            const index = this.data.locationArrNFC.findIndex((item) => {
                return item.id === id;
            });
            if (index > -1) {
                this.setData({
                    [`locationArrNFC[${index}].commentcount`]: commentcount,
                });
            }
        }
    },

    // 预览图片
    previewApnImage(e) {
        let { index, apnpoint } = e.currentTarget.dataset;

        apnpoint = JSON.parse(apnpoint);

        wx.previewImage({
            current: apnpoint[index].sceneImageArr[0],
            urls: apnpoint[index].sceneImageArr,
        });
    },

    // 复制
    copyText: function (e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.content,
            success: function (res) {
                wx.getClipboardData({
                    success: function (res) {},
                });
            },
        });
    },

    //打电话
    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone,
        });
    },
    //秘书团
    getHomePageInfo(e) {
        this.setData({
            questionAnswerNum: e.detail.questionAnswerNum,
        });
    },

    // 编辑运营商
    editOperator: throttle(
        (e) => {
            const { nav } = e.currentTarget.dataset;
            if (hasPermission(['00', '01', '02', '03', '04', '05', '06'])) {
                wx.navigateTo({
                    url: nav,
                });
            } else {
                wx.showToast({
                    title: '暂无权限',
                    icon: 'none',
                    duration: 2000,
                });
            }
        },
        200,
        {
            trailing: false,
        }
    ),

    getBusinesshallList() {
        let data = {
            authId: app.globalData.authId,
            city: app.globalData.addressBack,
            country: app.globalData.countryBack,
        };
        http.getRequest(`cjMap/getBusinesshallList`, data).then((res) => {
            if (res.status == 0) {
                res.data.map((item) => {
                    item.createtime = app.timeago(item.createtime);
                    item.distance = formatDistance(item.distance);
                    item.picurls = item.picurl ? JSON.parse(item.picurl) : [];
                    this.data.businesshallArr.push(item);
                });
                this.setData({
                    businesshallArr: this.data.businesshallArr,
                });
            }
        });
    },

    getPackageList() {
        let _that = this;
        let area = '';
        let country = app.globalData.countryBack;
        let city = app.globalData.addressBack;

        if (country == '中国') {
            if (['香港', '澳门'].includes(city)) {
                area = country + city;
            } else if (app.globalData.taiwan.includes(city)) {
                area = '中国台湾';
            } else {
                area = '中国大陆';
            }
        } else {
            area = country;
        }

        _that.setData({
            area,
        });
        let data = {
            authId: app.globalData.authId,
            city: app.globalData.addressBack,
            country: app.globalData.countryBack,
            area: area,
        };
        http.getRequest(`cjMap/getPackageList`, data).then((res) => {
            if (res.status == 0) {
                res.data.map((item) => {
                    item.createtime = getTimePart(item.createtime).date;
                    this.data.packageArr.push(item);
                });
                this.setData({
                    packageArr: this.data.packageArr,
                });
            }
        });
    },

    editPackage(e) {
        let { uid, id, index } = e.currentTarget.dataset;
        let actions = [
            {
                name: '编辑',
                color: '#333333',
                id: 2,
            },
            {
                name: '删除',
                color: '#FF455E',
                id: 1,
            },
        ];

        this.setData({
            show: true,
            activeCreateUid: uid,
            actions,
            activeIndex: index,
            packageId: id,
        });
    },

    deletePackage() {
        let data = {
            authId: app.globalData.authId,
            id: this.data.packageId,
        };
        http.getRequest('cjMap/deletePackage', data).then((res) => {
            if (res.status == 0) {
                this.data.packageArr.splice(this.data.activeIndex, 1);
                this.setData({
                    packageArr: this.data.packageArr,
                });
                // 刷新顶部数字点
                this.getTabsCount();
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1500,
                });
            }
        });
    },
    jumpInfoTap(e) {
        const { id, type, dotid, mtitle } = e.currentTarget.dataset;
        if (dotid || mtitle) {
            wx.showToast({
                title: '请到春军扫网App查看',
                icon: 'none',
                duration: 1500,
            });
            return;
        }
        let url = `../../functionPage/pages/cszxDetail/cszxDetail?id=${id}&type=${type}`;
        wx.navigateTo({
            url: url,
        });
    },
});
